﻿using System;
using System.Collections.Generic;
using System.Linq;

using Microsoft.Practices.Prism.ViewModel;

namespace Finance.UI.ViewModels
{
    public class SelectorPageViewModel : NotificationObject
    {
        public string _Header;
        public string Header
        {
            get { return _Header; }
            set
            {
                _Header = value;
                RaisePropertyChanged("Header");
            }
        }

        private IList<DetailGroupViewModel> _DetailGroupViewModels = new List<DetailGroupViewModel>();
        public IList<DetailGroupViewModel> DetailGroupViewModels
        {
            get { return _DetailGroupViewModels; }
            set
            {
                _DetailGroupViewModels = value;
                RaisePropertyChanged("DetailViewModels");
            }
        }

        public SelectorPageViewModel(
            string header,
            string selectedItemName = "",
            params DetailGroupViewModel[] detailGroupViewModels)
        {
            _DetailGroupViewModels.Clear();
            _Header = header;
            foreach (var groupViewModel in detailGroupViewModels)
            {
                foreach (var selectItem in
                    groupViewModel.Items.Where(itemViewModel => itemViewModel.Name == selectedItemName))
                {
                    selectItem.IsSelected = true;
                    _DetailGroupViewModels.Add(groupViewModel);
                }
            }
            foreach (var groupViewModel in
                detailGroupViewModels.Where(
                    groupViewModel => !groupViewModel.Items.Any(
                        itemViewModel => itemViewModel.Name == selectedItemName)))
            {
                _DetailGroupViewModels.Add(groupViewModel);
            }
        }
    }
}
